using System;
using System.Collections.Generic;
using System.Reflection;
using RikMigrations.Providers;

namespace RikMigrations
{
	public class MigrationManager
	{
		#region [rgn] Fields (3)
        public const string DefaultModuleName = "DEFAULT";

	    #endregion [rgn]

		#region [rgn] Methods (10)

		// [rgn] Public Methods (5)
		public static void MigrateTo(Assembly assembly, string module, int version)
		{
            MigrateTo(assembly, module, version, new MssqlProvider());
		}
		
		public static void MigrateTo(Assembly assembly, string module, int? version, DbProvider db)
		{
            string[] moduleAsArray = (module == null) ? null : new[] { module };
            CreateRunner(db, assembly).MigrateTo(moduleAsArray, version);
		}

	    private static MigrationRunner CreateRunner(DbProvider db, Assembly assembly)
	    {
	        return new MigrationRunner(db, FindAllMigrations(assembly));
	    }

	    public static void UpgradeMax(Assembly assembly)
		{
		    MigrateTo(assembly, null, null, new MssqlProvider());
		}
		
		public static void UpgradeMax(Assembly assembly, DbProvider db, params string[] moduleNames)
		{
            CreateRunner(db, assembly).MigrateTo( moduleNames , null);
        }

	    #endregion [rgn]

	    public static Dictionary<string, List<MigrationAttribute>> FindAllMigrations(Assembly assembly)
	    {
	        return new MigrationSearch().Search(assembly);
	    }
	}
}